package com.zhika.zishi.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.beans.ItemLetterBean;

import java.util.ArrayList;

/**
 * Created by shi-02 on 2015/4/19.
 */
public class ItemLetterAdapter extends BaseAdapter {

    private Context context;

    private ArrayList<ItemLetterBean> letterList;

    public ItemLetterAdapter(Context context,ArrayList<ItemLetterBean> letterList){
        this.context = context;

        this.letterList = letterList;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getCount() {
        return letterList.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;

        if (convertView == null){
            view = LayoutInflater.from(context).inflate(R.layout.item_of_letter_fragment,null);
            viewHolder = new ViewHolder();
            viewHolder.headerImageView = (ImageView) view.findViewById(R.id.item_of_letter_fragment_header_image_view);
            viewHolder.nicknameText =(TextView) view.findViewById(R.id.item_of_letter_fragment_nickname_text);
            viewHolder.contentText = (TextView) view.findViewById(R.id.item_of_letter_fragment_content_text);

            view.setTag(viewHolder);

        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
            resetViewHolder(viewHolder);
        }

        ItemLetterBean bean = letterList.get(position);

        viewHolder.headerImageView.setImageResource(R.drawable.ic_launcher);
        viewHolder.nicknameText.setText(bean.getNicknameStr());
        viewHolder.contentText.setText(bean.getLetterContent());

        return view;
    }

    private void resetViewHolder(ViewHolder holder){
        holder.headerImageView.setImageBitmap(null);

        holder.nicknameText.setText(null);

        holder.contentText.setText(null);

    }
    private class ViewHolder{
        private ImageView headerImageView;

        private TextView nicknameText;

        private TextView contentText;
    }
}
